package com.sneaker.shower.socket.feature;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.sneaker.shower.socket.Group;
import com.sneaker.shower.socket.repository.GroupRepository;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Vector;

/**
 * <h1>ListGroup</h1>
 * <pre>
 * @Description
 * </pre>
 *
 * @Author lopo
 * @Date 2018/10/19
 */
public class ListGroup<T> extends DelegateSignAble implements Group<T> {
    static GroupRepository groupRepository;
    private List<T> group = new Vector<>();

    public ListGroup(SignAble signAble) {
        super(signAble);
    }

    @Override
    @JsonProperty("group")
    public Collection<T> group() {
        return group;
    }

    @Override
    public void add(T t) {
        group.add(t);
    }

    @Override
    public void remove(T t) {
        group.remove(t);
        if (group.isEmpty()) {
            broken();
        }
    }

    @Override
    public void start() {
    }

    @Override
    public void valid() {
    }

    @Override
    public void invalid() {
    }

    @Override
    public void broken() {
        groupRepository.delete(this);
    }

    @Override
    @JsonProperty("sign")
    public Serializable sign() {
        return super.sign();
    }

    @Override
    public String toString() {
        return "ListGroup{" +
                "group=" + group +
                '}';
    }
}
